package com.qdlc.p2p.biz.processor.task;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qdlc.p2p.biz.executer.ExecuterHelper;
import com.qdlc.p2p.biz.executer.impl.ScoreLotteryMoneyExecuter;
import com.qdlc.p2p.biz.log.score.BaseScoreLog;
import com.qdlc.p2p.biz.log.score.lottery.ScoreLotteryLog;
import com.qdlc.p2p.biz.processor.event.LotteryScoreValueEvent;
import com.qdlc.p2p.biz.service.lottery.LotteryTaskService;
import com.qdlc.p2p.biz.tpp.TppBaseInterface;
import com.qdlc.p2p.biz.tpp.TppServiceFactory;
import com.qdlc.p2p.biz.zdata.LotteryResult;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.core.queue.TaskProcessorException;
import com.qdlc.p2p.common.core.queue.ValueEvent;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.dao.ScoreTypeDao;
import com.qdlc.p2p.dal.dto.Tpp;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.ScoreLotteryModel;
import com.qdlc.p2p.dal.model.TppModel;
import com.qdlc.p2p.dal.util.SystemConfigs;

/**
 * Created by hongbl on 2016/2/2.
 */
@Service
@Transactional
public class LotteryScoreTaskProcessor  extends AbstractTaskProcessor{
    @Resource
    private ScoreTypeDao scoreTypeDao;
    @Resource
    private LotteryTaskService lotteryTaskService;
    /** 积分抽奖 */
    private static final String SCORE_LOTTERY = "score_lottery";

    @Override
    protected void process0(ValueEvent event) throws TaskProcessorException {
        LotteryScoreValueEvent lotteryScoreEvent = (LotteryScoreValueEvent)event;
        User user = lotteryScoreEvent.getUser();
        LotteryResult lotteryResult = lotteryScoreEvent.getLotteryResult();
        //获取消耗的积分
        int score=scoreTypeDao.getScoreTypeByNid(SCORE_LOTTERY).getValue();
        Global.setTransfer("score", score);
        Global.setTransfer("name", lotteryResult.getName());
        BaseScoreLog bLog = new ScoreLotteryLog(user.getUserId(), score);
        bLog.doEvent();
        if (lotteryResult.getType() == ScoreLotteryModel.TYPE_SCORE) {
            //会员积分记录信息 更新积分， 更新日志信息
            lotteryTaskService.giveScore(user.getUserId(),score);
        } else if (lotteryResult.getType() == ScoreLotteryModel.TYPE_MONEY) {
            //抽中现金
            double money = lotteryResult.getValue();
            Global.setTransfer("money", money);
            Global.setTransfer("user", user);
            ScoreLotteryMoneyExecuter scoreLotteryMoneyExecuter = ExecuterHelper.getExecuter(ScoreLotteryMoneyExecuter.class);
            scoreLotteryMoneyExecuter.execute(money, user, new User(SystemConfigs.ADMIN_ID));

            Tpp tpp = new Tpp(TppModel.SCORE_LOTTERY_MONEY, TppModel.TPP_TRANSFER, null, user, money, null);
            tpp.setTppUserCustId(SystemConfigHelper.getValue(SystemConfigHelper.Nid.MERCHANT_ID));
            tpp.setTppBorrowId(TppModel.SCORE_LOTTERY_MONEY_NAME);
            TppModel tppModel = TppModel.instance(tpp);
            TppBaseInterface tppService = TppServiceFactory.newIntance();
            tppService.tppTransfer(tppModel);
        }
    }

    @Override
    public boolean supports(ValueEvent event) {
        return event instanceof LotteryScoreValueEvent;
    }

}
